package com.thingple.web.bridge.location;

import android.content.Context;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.baidu.location.BDLocation;
import com.thingple.h5.bridge.AbstractJsBridge;
import com.thingple.h5.bridge.BridgeConfig;
import com.thingple.location.LocationFactory;
import com.thingple.location.LocationRecord;

/**
 *
 * Created by lism on 2017/10/19.
 */
@BridgeConfig(moduleName = "loc")
public class LocationJsBridgeImpl extends AbstractJsBridge implements LocationJsBridge {

    public LocationJsBridgeImpl(WebView webView, Context context) {
        super(webView, context);
    }

    @Override
    public void destroy() {

    }

    @JavascriptInterface
    @Override
    public String getLocation() {
        LocationRecord location = LocationFactory.shareInstance().getLocation();
        if (location != null) {
            long last = System.currentTimeMillis() - location.lastUpdateTime();
            if (last < 10000) {// 10second
                BDLocation loc = location.getLocation();
                return loc.getLongitude() + "," + loc.getLatitude();
            }
        }
        return null;
    }
}
